/******************************************************************/
//	Copyright (C), 2011-2013, �������� 
//  Author   	  	: ����Ԫ  
//  Reviser				: �����
//  Update Date   : 2012/08/01
//  Version   	  : 1.3          
//  Description   : Transplant to v3.5 function library
/******************************************************************/ 

/* Includes ------------------------------------------------------------------*/
#include "STM32Lib\\stm32f10x.h"
#include "UP_Timer.h"
#include "UP_UART.h"
#include "UP_Globle.h"
#include "senser_state.h"
#include "car.h"
#include "sys.h"
#include "UP_Exti.h"
#include "UP_ADC.h"
#include "task.h"
#include "motor.h"

extern PID_t pRobot;
extern uint16_t Timekeeping;
extern uint16_t Count;
extern uint16_t NowValue;
extern uint8_t s;

/* Private typedef -----------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define countof(a)   (sizeof(a) / sizeof(*(a)))

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : NMIException
* Description    : This function handles NMI exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NMIException(void)
{
}

/*******************************************************************************
* Function Name  : HardFaultException
* Description    : This function handles Hard Fault exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void HardFaultException(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : MemManageException
* Description    : This function handles Memory Manage exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MemManageException(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : BusFaultException
* Description    : This function handles Bus Fault exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void BusFaultException(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : UsageFaultException
* Description    : This function handles Usage Fault exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void UsageFaultException(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : DebugMonitor
* Description    : This function handles Debug Monitor exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void DebugMonitor(void)
{
}

/*******************************************************************************
* Function Name  : SVCHandler
* Description    : This function handles SVCall exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SVCHandler(void)
{
}

/*******************************************************************************
* Function Name  : PendSVC
* Description    : This function handles PendSVC exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void PendSVC(void)
{
}

/*******************************************************************************
* Function Name  : SysTickHandler
* Description    :ϵͳʱ�ӣ�һ�����1MS�ж�һ��
*******************************************************************************/
extern void CheckCDSSend(void);
void SysTick_Handler(void)		//����Ƿ��ж������֡��Ҫ���ͣ�ÿ1ms���һ�Σ�
{
	CheckCDSSend();
}

//void EXTI_IRQHandler(u32 Channel)
//{
//	if(g_UP_ExtiITAddress != 0)
//		(*((void(*)(u32))g_UP_ExtiITAddress))(Channel);
//	EXTI->PR = Channel;		//�����־
//}

/*后面两个光电开关*/
//void EXTI0_IRQHandler(void)
//{ 
//	UP_delay_ms(1);
//	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)==0)
//	{
//		if(UP_ADC_GetValue(4)>ENEMYDISTANCE||UP_ADC_GetValue(5)>ENEMYDISTANCE)
//		{
//			attack_sta=1;
//		}
//	}
//	EXTI->PR=EXTI_Line0;	//清除标志位
//}

//void EXTI1_IRQHandler(void)
//{
//	UP_delay_ms(1);
//	if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)==0)
//	{
//		if(UP_ADC_GetValue(4)>ENEMYDISTANCE||UP_ADC_GetValue(5)>ENEMYDISTANCE)
//		{
//			attack_sta=1;
//		}
//	}
//	EXTI->PR=EXTI_Line1;	//清除标志位
//}

/*底部的两个光电模块*/
void EXTI4_IRQHandler(void)
{
	UP_delay_ms(1);
	//判断是否触发中断，并且在台上
	if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)==COVERON&&RestartFlag==RESETFLAG)
	{
		s=SETFLAG;
//		Count=CLEARCOUNT;			//清零计次
		Timekeeping=CLEARCOUNT;		//清零计时
//		NowValue=CLEARCOUNT;		//清零当前灰度值
		//可通过标志位判断然后加速
		if(UP_ADC_GetValue(2)<EGEMDISTANCE)	//在边缘时检测前方是否有敌人（面向擂台外）
		{
			UP_sta=RESETFLAG;		//置0标志位
			car_control(high_reteat);
			UP_delay_ms(350);
			car_control(turn_left);
			UP_delay_ms(240);
		}
		else
		{
			UP_sta=SETFLAG;				//置标志位
			car_control(high_reteat);	//防止敌方在此处重新上台
			UP_delay_ms(250);
			car_control(brake);			//避免翘头
			UP_delay_ms(120);
		}
	}
	EXTI->PR=EXTI_Line4;	//清除标志位
}

void EXTI9_5_IRQHandler(void)
{
	UP_delay_ms(1);
	//判断是否触发中断，并且在台上
	if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)==COVERON&&RestartFlag==RESETFLAG)
	{
		s=SETFLAG;
//		Count=CLEARCOUNT;			//清零计次
		Timekeeping=CLEARCOUNT;		//清零计时
//		NowValue=CLEARCOUNT;		//清零当前灰度值
		if(UP_ADC_GetValue(2)<EGEMDISTANCE)	//在边缘时检测前方是否有敌人（面向擂台外）
		{
			UP_sta=RESETFLAG;		//置0标志位
			car_control(high_reteat);
			UP_delay_ms(350);
			car_control(turn_right);
			UP_delay_ms(240);
		}
		else
		{
			UP_sta=SETFLAG;				//置标志位
			car_control(high_reteat);	//防止敌方在此处重新上台
			UP_delay_ms(250);
			car_control(brake);			//避免翘头
			UP_delay_ms(120);
		}
	}
	EXTI->PR=EXTI_Line5;	//清除标志位
}







//void EXTI9_5_IRQHandler(void)
//{
//	if(EXTI_GetITStatus(EXTI_Line5) != RESET)
//		EXTI_IRQHandler(EXTI_Line5);
//	else if(EXTI_GetITStatus(EXTI_Line6) != RESET)
//		EXTI_IRQHandler(EXTI_Line6);
//	else if(EXTI_GetITStatus(EXTI_Line7) != RESET)
//		EXTI_IRQHandler(EXTI_Line7);
//	else if(EXTI_GetITStatus(EXTI_Line8) != RESET)
//		EXTI_IRQHandler(EXTI_Line8);
//	else if(EXTI_GetITStatus(EXTI_Line9) != RESET)
//		EXTI_IRQHandler(EXTI_Line9);
//}

//void EXTI15_10_IRQHandler(void)
//{
//	if(EXTI_GetITStatus(EXTI_Line10) != RESET)
//		EXTI_IRQHandler(EXTI_Line10);
//	else if(EXTI_GetITStatus(EXTI_Line11) != RESET)
//		EXTI_IRQHandler(EXTI_Line11);
//	else if(EXTI_GetITStatus(EXTI_Line12) != RESET)
//		EXTI_IRQHandler(EXTI_Line12);
//	else if(EXTI_GetITStatus(EXTI_Line13) != RESET)
//		EXTI_IRQHandler(EXTI_Line13);
//	else if(EXTI_GetITStatus(EXTI_Line14) != RESET)
//		EXTI_IRQHandler(EXTI_Line14);
//	else if(EXTI_GetITStatus(EXTI_Line15) != RESET)
//		EXTI_IRQHandler(EXTI_Line15);
//}

//extern volatile bool ADC_Ok;
//void DMA1_Channel1_IRQHandler(void)
//{
//	if(DMA_GetITStatus(DMA1_IT_TC1))
// 	{
//		DMA_ClearITPendingBit(DMA1_IT_GL1);	//���ȫ���жϱ�־
//	//	ADC_Ok=TRUE;
//	}
//}
/*
extern void UP_Sev_TimerPro(void);
void TIM2_IRQHandler(void)
{
	UP_Sev_TimerPro();
}
*/

void TIM3_IRQHandler(void)
{	
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)	//获取中断标志位
	{	
		PID_Calculate(&pRobot);			//pid计算
#ifdef INCREMENTAL		
		/*累加速度*/
		pRobot.LeftSpeed=MINSPEED+pRobot.Out;	
		pRobot.RightSpeed=MAXSPEED+pRobot.Out;
#elif defined POSITION
		/*累加速度*/
		pRobot.LeftSpeed+=pRobot.Out;	
		pRobot.RightSpeed+=pRobot.Out;
#endif
		/*速度限幅*/
		if(pRobot.LeftSpeed>MAXSPEED)
		{
			pRobot.LeftSpeed=MAXSPEED;
		}
		else if(pRobot.LeftSpeed<MINSPEED)
		{
			pRobot.LeftSpeed=MINSPEED;
		}
		if(pRobot.RightSpeed>MAXSPEED)
		{
			pRobot.RightSpeed=MAXSPEED;
		}
		else if(pRobot.RightSpeed<MINSPEED)
		{
			pRobot.RightSpeed=MINSPEED;
		}			
		//设定电机速度
		motor_control(pRobot.LeftSpeed,pRobot.LeftSpeed,pRobot.RightSpeed,pRobot.RightSpeed);
		if(pRobot.Actual==1||pRobot.Actual==-1)UP_delay_ms(50);
		if(pRobot.Actual==2||pRobot.Actual==-2)UP_delay_ms(100);
		if(pRobot.Actual==3||pRobot.Actual==-3)UP_delay_ms(200);
		if(pRobot.Actual==4||pRobot.Actual==-4)UP_delay_ms(300);
		if(pRobot.Actual==5||pRobot.Actual==-5)UP_delay_ms(400);
		if(pRobot.Actual==6||pRobot.Actual==-6)UP_delay_ms(450);
	}
	TIM3->SR = (uint16_t)~TIM_IT_Update;	//清除中断标志位
}

/*
extern void UP_PWM_TimerPro(void);
void TIM4_IRQHandler(void)
{	
	UP_PWM_TimerPro();
}  // */

//void USART1_IRQHandler(void)
//{
//	//�����ж�
//	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
//	{
//		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
//		if(g_UP_BluetoothITAddress != 0)
//			(*((void(*)(u8))g_UP_BluetoothITAddress))(USART_ReceiveData(USART1));
//	}
//	
//	//���-������������Ҫ�ȶ�SR,�ٶ�DR�Ĵ��� �������������жϵ�����
//	if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
//	{
//		USART_ClearFlag(USART1,USART_FLAG_ORE);	//��SR
//		USART_ReceiveData(USART1);				//��DR
//	}
//}

//u16 rd=0;

//void UART5_IRQHandler(void)
//{
//	//�����ж�
//	if(USART_GetITStatus(UART5,USART_IT_RXNE)==SET)
//	{
//		USART_ClearITPendingBit(UART5,USART_IT_RXNE);
//		if(g_UP_USR232ITAddress != 0)
//			(*((void(*)(u8))g_UP_USR232ITAddress))(USART_ReceiveData(UART5));
//	}
//	
//	
//	//���-������������Ҫ�ȶ�SR,�ٶ�DR�Ĵ��� �������������жϵ�����
//	if(USART_GetFlagStatus(UART5,USART_FLAG_ORE)==SET)
//	{
//		USART_ClearFlag(UART5,USART_FLAG_ORE);	//��SR
//		USART_ReceiveData(UART5);				//��DR
//	}
//	rd=USART_ReceiveData(UART5);
//	UP_LCD_ShowInt(0,0,rd);
//	UP_delay_ms(500);
//}
//extern void UP_Zigbee_Rec(u32 data);
//u32 count=0;
//void UART4_IRQHandler(void)
//{
//	//�����ж�
//	if(USART_GetITStatus(UART4,USART_IT_RXNE)==SET)
//	{
//		USART_ClearITPendingBit(UART4,USART_IT_RXNE);
// 		if(g_UP_ZigbeeITAddress != 0)
// 			(*((void(*)(u8))g_UP_ZigbeeITAddress))(USART_ReceiveData(UART4));
//		//UP_Zigbee_Rec((u32)USART_ReceiveData(UART4));
//		//count++;
//	}
//	//���-������������Ҫ�ȶ�SR,�ٶ�DR�Ĵ��� �������������жϵ�����
//	if(USART_GetFlagStatus(UART4,USART_FLAG_ORE)==SET)
//	{
//		USART_ClearFlag(UART4,USART_FLAG_ORE);	//��SR
//		USART_ReceiveData(UART4);				//��DR
//	}
//}

void DMA1_Channel2_IRQHandler(void)
{
	if((DMA1->ISR & DMA1_IT_TC2) != (uint32_t)RESET)
 	{	
		DMA1->IFCR = DMA1_IT_GL2;				//����жϱ�־
		DMA1_Channel2->CCR &= ~((u32)0x01);		//����DMA
		UP_delay_us(100);
		UART3_TXDS;
	//	DMA1_Channel2->CNDTR = 15;				//����װ��Ҫ���͵��ֽ���	// */
	}
}

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
